<?php
/**
 * 企业管理
 */
namespace Weixin\Controller;

use Common\Controller\WeixinbaseController;

class ComsController extends WeixinbaseController
{
    protected $wx_coms;
    protected $wx_person;
    protected $wx_user;
    protected $chinaCity_model;
    protected $page_size;//分页大小

    public function _initialize()
    {
        $this->wx_coms = D("Common/Coms");
        $this->wx_person = D("Common/Person");
        $this->wx_user = D("Common/WxUser");
        $this->chinaCity_model = D("Common/ChinaCity");
        $this->page_size = 15;
        parent::_initialize();
    }


    public function center(){
        $this->assign("user", $this->getUserInfo());
        $this->display();
    }
    /**
     * 企业列表
     */
    public function index()
    {
        $where = "com_state = 1";
        //获取当前用户经纬度
        $uid =  empty($_SESSION['uid'])? 0 : $_SESSION['uid'];
        if ($uid != 0) {
            $wx_user = $this->wx_user->where("uid='$uid'")->find();
            if (!empty($wx_user) && !empty($wx_user['lat']) && !empty($wx_user['lon'])) {
                $city_json = get_baidu_map_by_lat_and_lng($wx_user['lat'], $wx_user['lon']);
                if ($city_json['state'] && !empty($city_json['city_name'])) {
                    $t_city_name = mb_substr($city_json['city_name'], 0, 2, 'utf-8');
                    $city_arr = $this->chinaCity_model->where([
                            'name' => [
                                'like', "%$t_city_name%"
                            ],
                            'level'=>2
                        ]
                    )->select();
                    if (!empty($city_arr)) {
                        $city = $city_arr[0];
//                         $where['city'] = $city['id'];
                        $where.=" and city=".$city['id'];
                        $this->assign('city_name', $city['name']);
                        $this->assign('city_id', $city['id']);
                    }
                }
            }
        }
         $where .= ' or is_top=1';
        $list = $this->wx_coms->field("id,com_name,pay,job_type,com_give,province,city,district,is_top,is_nojob,onjob_day")
            ->order("is_top desc , created_time DESC")
            ->where($where)
            ->limit(0 . ',' . $this->page_size)
            ->select();
        $sql = $this->wx_coms->getLastSql();
        if (count($list) < $this->page_size) {
            $has_more = 0;
        } else {
            $has_more = 1;
        }
        foreach ($list as $k=>$v){
        	$list[$k][com_name] = msubstrBySelf($v[com_name], 0,15) ;
        }
        
        $cache_city_select = F('cache_city_select');
        $this->assign('now_page_size', $this->page_size);
        $this->assign('cache_city_select', $cache_city_select);
        $this->assign('list', $list);
        $this->assign('has_more', $has_more);
        $this->assign("user", $this->getUserInfo());
        $this->display();
    }

    /**
     * ajax 获取用户总金额与用户工友数量
     */
    public function get_user_info_num()
    {
        if (IS_AJAX) {
            $user_info = $this->getUserInfo();
            $this->ajaxReturn([
                'invite_count' => $user_info['invite_count'],
                'total_money' => $user_info['total_money'],
            ]);
        }
    }


    /**
     * ajax 分页
     */
    public function get_com_list()
    {
        if (IS_AJAX) {
            $where = "com_state = 1";
            $pageSize = I('request.now_page_size', 0, 'intval'); //微信页面当前的显示数量
            $city_id = I('request.city_id'); //微信页面当前的显示数量
            if ($city_id) {
                 	$where.=" and city=".$city_id;
            }
            $where.=" or is_top=1";
            $list = $this->wx_coms->field("id,com_name,pay,job_type,com_give,province,city,district,is_top,is_nojob")
                ->where($where)
                ->order("is_top desc,created_time DESC")
                ->limit($pageSize . ',' . $this->page_size)
                ->select();
            if (count($list) < $this->page_size) {
                $has_more = 0;
            } else {
                $has_more = 1;
            }
            $this->assign('list', $list);
            $content = $this->fetch('coms');
            $this->ajaxReturn(array(
                'now_page_size' => $this->page_size + $pageSize,
                'content' => $content,
                'has_more' => $has_more
            ));
        }
    }

    /**
     * 企业详情
     */
    public function detail()
    {
        $id = I("get.id", 0, 'intval');
        $data = $this->wx_coms->where(array("id" => $id))->find();
        $data[com_name] = msubstrBySelf($data[com_name], 0,25);
        switch ($data['salaryday']){
        	case -1:
        		$data['salaryday'] =  "月底";
        		break;
        	case 0:
        		$data['salaryday'] =  "企业代发";
        		break;
        	default:
        		$data['salaryday'] =  $data['salaryday']."号";
        }
        $this->assign("data", $data);
        $this->display();
    }

    /**
     * 申请入职
     */
    public function apply_job()
    {
      //  $this->check_is_invite();
        $com_id = I("get.id", 0, 'intval');
        $this->assign("com_id", $com_id);
        parent::dict_radio(array(array(
            'code' => 'USER_SEX',
            'name' => 'sex',
            'text' => '性别'
        )));
        $this->display();
    }

    /**
     * 申请入职提交
     */
    public function apply_job_post()
    {
        //$this->check_is_invite();
        $com_id = I("post.com_id", 0, 'intval');
        $name = $_POST['name'];
        $card = $_POST['card'];
        $mobile = $_POST['mobile'];
        $sex = $_POST['sex'];
        if (empty($com_id) || empty($name) || empty($card) || empty($mobile) || empty($sex)) {
            $result['state'] = -1;
            $result['msg'] = "必填信息不能为空!";
        } else {
            if (!$this->validation_filter_id_card($card)) {
                $result['state'] = -1;
                $result['msg'] = "身份证号格式不正确!";
            } elseif (!$this->is_mobile_phone($mobile)) {
                $result['state'] = -1;
                $result['msg'] = "手机号格式不正确!";
            } else {
                $where['p.card'] = $card;
                $where['a.com_id'] = $com_id;
                $where['a.job_state'] = array('in', '1,3');
                $info = $this->wx_person->alias("p")
                    ->join("__APPLY_JOB__ as a ON a.person_id = p.id")
                    ->where($where)->field("p.id")->find();
                if (empty($info)) {
                    $invite_id = empty($_SESSION['uid'])? $_SESSION['personID']: $_SESSION['uid'] ;
                    $person['name'] = $name;
                    $person['card'] = $card;
                    $person['mobile'] = $mobile;
                    $person['sex'] = $sex;
                    $person['created_timie'] = time();
                    /*判断身份证号码，不存在做add操作**/
                    $t =  $this->wx_person->where(array("card"=>$card))->find();
                    if(empty($t)){
                    	$person_id = $this->wx_person->add($person);
                    }else{
	                    $person_id = $t['id'];
                    }
                    if ($person_id !== false) {
                        $apply_job['com_id'] = $com_id;
                        $apply_job['person_id'] = $person_id;
                        $apply_job['invite_id'] = $invite_id;
                        $apply_job['created_time'] = time();
                        $apply_job['last_time'] = time();
                        $apply_job['state'] = 1; /**1：正常 2：删除***/
                        $this->wx_apply_job->add($apply_job);
                        $result['state'] = 1;
                        $result['msg'] = "您的信息已经提交成功！";
                    }
                } else {
                    $result['state'] = -1;
                    $result['msg'] = "该身份证号码在该企业<br/>正在申请中或已在职！";
                }
            }
        }
        echo json_encode($result, true);
    }
}

